package com.campus.activity.controller;

import com.campus.activity.common.Result;
import com.campus.activity.entity.Activity;
import com.campus.activity.service.ActivityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 活动管理控制器
 * 
 * @author System
 * @since 2024
 */
@RestController
@RequestMapping("/api/activities")
@Tag(name = "活动管理", description = "活动信息的增删改查和发布管理")
@Validated
@CrossOrigin(origins = "*")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @PostMapping
    @Operation(summary = "添加活动", description = "创建新的活动信息")
    public Result<Boolean> addActivity(@Valid @RequestBody Activity activity) {
        boolean success = activityService.addActivity(activity);
        return success ? Result.success("活动添加成功", true) : Result.error("活动添加失败");
    }

    @PutMapping("/{activityId}")
    @Operation(summary = "更新活动", description = "更新指定活动的信息")
    public Result<Boolean> updateActivity(
            @Parameter(description = "活动ID") @PathVariable Integer activityId,
            @Valid @RequestBody Activity activity) {
        boolean success = activityService.updateActivity(activityId, activity);
        return success ? Result.success("活动更新成功", true) : Result.error("活动更新失败");
    }

    @DeleteMapping("/{activityId}")
    @Operation(summary = "删除活动", description = "删除指定的活动")
    public Result<Boolean> deleteActivity(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        boolean success = activityService.deleteActivity(activityId);
        return success ? Result.success("活动删除成功", true) : Result.error("活动删除失败");
    }

    @GetMapping("/{activityId}")
    @Operation(summary = "获取活动详情", description = "根据活动ID获取活动详细信息")
    public Result<Activity> getActivity(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        Activity activity = activityService.getById(activityId);
        return activity != null ? Result.success(activity) : Result.notFound();
    }

    @GetMapping
    @Operation(summary = "查询活动列表", description = "根据条件查询活动列表")
    public Result<List<Activity>> queryActivities(
            @Parameter(description = "活动类型") @RequestParam(required = false) String activityType,
            @Parameter(description = "开始时间") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) 
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
            @Parameter(description = "活动地点") @RequestParam(required = false) String location) {
        List<Activity> activities = activityService.queryActivities(activityType, startTime, endTime, location);
        return Result.success(activities);
    }

    @GetMapping("/type/{activityType}")
    @Operation(summary = "按类型查询活动", description = "根据活动类型查询活动列表")
    public Result<List<Activity>> getActivitiesByType(
            @Parameter(description = "活动类型") @PathVariable String activityType) {
        List<Activity> activities = activityService.getActivitiesByType(activityType);
        return Result.success(activities);
    }

    @GetMapping("/location/{location}")
    @Operation(summary = "按地点查询活动", description = "根据活动地点查询活动列表")
    public Result<List<Activity>> getActivitiesByLocation(
            @Parameter(description = "活动地点") @PathVariable String location) {
        List<Activity> activities = activityService.getActivitiesByLocation(location);
        return Result.success(activities);
    }

    @GetMapping("/organizer/{organizerId}")
    @Operation(summary = "按组织者查询活动", description = "根据组织者ID查询活动列表")
    public Result<List<Activity>> getActivitiesByOrganizer(
            @Parameter(description = "组织者ID") @PathVariable Integer organizerId) {
        List<Activity> activities = activityService.getActivitiesByOrganizerId(organizerId);
        return Result.success(activities);
    }

    @GetMapping("/open")
    @Operation(summary = "获取开放活动", description = "获取所有开放报名的活动")
    public Result<List<Activity>> getAllOpenActivities() {
        List<Activity> activities = activityService.getAllOpenActivities();
        return Result.success(activities);
    }

    @GetMapping("/upcoming-deadline")
    @Operation(summary = "即将截止活动", description = "获取即将截止报名的活动")
    public Result<List<Activity>> getUpcomingDeadlineActivities() {
        List<Activity> activities = activityService.getUpcomingDeadlineActivities();
        return Result.success(activities);
    }

    @PostMapping("/publish")
    @Operation(summary = "发布活动", description = "发布新的活动")
    public Result<Boolean> publishActivity(@Valid @RequestBody Activity activity) {
        boolean success = activityService.publishActivity(activity);
        return success ? Result.success("活动发布成功", true) : Result.error("活动发布失败");
    }

    @GetMapping("/popular")
    @Operation(summary = "获取热门活动", description = "获取热门活动列表")
    public Result<List<Activity>> getPopularActivities() {
        List<Activity> activities = activityService.getPopularActivities();
        return Result.success(activities);
    }

    @GetMapping("/latest")
    @Operation(summary = "获取最新活动", description = "获取最新发布的活动")
    public Result<List<Activity>> getLatestActivities(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "5") Integer limit) {
        List<Activity> activities = activityService.getLatestActivities(limit);
        return Result.success(activities);
    }
}